home *** CD-ROM | disk | FTP | other *** search
/ Electronic Clipper 1995 April / Electronic Clipper 1995-04.iso / mac / PC_USERS / IDEASRC / SETUP / MPLAYER / PLAYMAIN.C < prev    next >
Text File  |  1993-04-17  |  17KB  |  475 lines

  1.  
  2. // ---------------------------------------------------------------------
  3. //
  4. // PlayMain.c - Movie Player - QuickTime for Windows
  5. //
  6. //              Version 1.0
  7. //
  8. //              (c) 1988-1992 Apple Computer, Inc. All Rights Reserved.
  9. //
  10. // ---------------------------------------------------------------------
  11.  
  12.  
  13. // Includes
  14. // --------
  15.    #include <Windows.h>                // Required by Windows
  16.  
  17.    #include <qtw.h>                    // Interface to QuickTime
  18.    #include <qtole.h>                  // Interface to qtole dll's
  19.  
  20.    #include "common.h"                 // Interface to common.c routines
  21.  
  22.    #include "player.h"                 // Interface to other *.c files
  23.    #include "player.hr"                // Defines used in *.rc files
  24.  
  25.  
  26. // Message-Persistent Data
  27. // -----------------------
  28.    static struct                          // Hungarian notation: g
  29.      {HINSTANCE      hInstance;           // Instance handle
  30.       HINSTANCE      hResources;          // Resource-only DLL handle
  31.       HWND           hwndFrame;           // Frame window handle
  32.       HWND           hwndClient;          // MDI client window
  33.       HMENU          hMenu;               // Frame window menu
  34.       HACCEL         hAccel;              // Frame window accelerators
  35.       HWND           hActiveModelessDlg;  // Handle of active modeless dlg if any
  36.       QTOLE_OLEDATA  qtoleOleData;        // OLE data struct
  37.  
  38.      } g;
  39.  
  40.  
  41. // Internal Function Declarations
  42. // ------------------------------
  43.    static LPSTR NEAR PlayerParseCmdLine    (LPSTR);
  44.    static  BOOL NEAR PlayerInitAppl        (VOID);
  45.    static  HWND NEAR PlayerInitInst        (HINSTANCE, LPSTR, int);
  46.    static  LONG NEAR PlayerTerminateInst   (VOID);
  47.    static  BOOL NEAR DoQuickTimeInit       (HINSTANCE, LPSTR, LPINT);
  48.    static  VOID NEAR KillQuickTime         (VOID);
  49.  
  50.  
  51. // Function: WinMain - Required Windows "Main" Routine
  52. // --------------------------------------------------------------------
  53. // Parameters: As required by Microsoft Windows
  54. //
  55. // Returns:    As required by Microsoft Windows
  56. // --------------------------------------------------------------------
  57.    int PASCAL WinMain (HINSTANCE hInstance,
  58.                     HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
  59.  
  60.    // Define function data
  61.  
  62.      {MSG  msg;                       // Message
  63.  
  64.       g.hInstance = hInstance;        // Initialize global data
  65.  
  66.    // Load resource-only DLL
  67.       if( !(g.hResources = CommonGetLocalizedResources
  68.                ( PLAYER_ROOT_NAME, hInstance, PLAYER_STRING_NOMEMORY )))
  69.          return 0;
  70.  
  71.    // Perform one-time initialization
  72.       if ( !hPrevInstance && !PlayerInitAppl() )
  73.          return 0;
  74.  
  75.    // Perform initializations that apply to a specific instance and create
  76.    // create window
  77.       if ( !PlayerInitInst( hPrevInstance, lpCmdLine, nCmdShow ))
  78.          return 0;
  79.  
  80.    // Main message loop
  81.       while (GetMessage (&msg, NULL, NULL, NULL))
  82.          {if( !g.hActiveModelessDlg ||
  83.                         !IsDialogMessage( g.hActiveModelessDlg, &msg ))
  84.              {if( !g.hwndClient ||
  85.                      !TranslateMDISysAccel( g.hwndClient, &msg ))
  86.                  {if( !g.hwndFrame ||
  87.                       !TranslateAccelerator( g.hwndFrame, g.hAccel, &msg ))
  88.                      {TranslateMessage (&msg);
  89.                       DispatchMessage  (&msg);
  90.                      }
  91.                  }
  92.              }
  93.          }
  94.  
  95.    // Cleanup movie player
  96.  
  97.       PlayerTerminateInst();
  98.  
  99.       return msg.wParam;
  100.      }
  101.  
  102.  
  103. // Function: PlayerInitAppl - Perform One-time Initialization
  104. // --------------------------------------------------------------------
  105. // Parameters: None
  106. //
  107. // Returns:    TRUE if OK, else FALSE
  108. // --------------------------------------------------------------------
  109.    static BOOL NEAR PlayerInitAppl ( VOID )
  110.  
  111.      {WNDCLASS wc;                     // Window class information
  112.  
  113.    // Register the frame (main) window class
  114.  
  115.       wc.style         = 0;
  116.       wc.lpfnWndProc   = PlayerFrameWndProc;
  117.       wc.cbClsExtra    = 0;
  118.       wc.cbWndExtra    = 0;
  119.       wc.hInstance     = g.hInstance;
  120.       wc.hIcon         = LoadIcon( g.hResources,
  121.                                   MAKEINTRESOURCE( PLAYER_PLAYER_ICON ));
  122.       wc.hCursor       = LoadCursor( NULL, IDC_ARROW );
  123.       wc.hbrBackground = (HBRUSH) ( COLOR_APPWORKSPACE + 1 );
  124.       wc.lpszMenuName  = NULL;
  125.       wc.lpszClassName = PLAYER_FRAME_CLASS;
  126.  
  127.       if( !RegisterClass( &wc ))
  128.           return FALSE;
  129.  
  130.    // Register the movie window class
  131.  
  132.       wc.style         = CS_HREDRAW | CS_VREDRAW | CS_DBLCLKS;
  133.       wc.lpfnWndProc   = PlayerMovieWndProc;
  134.       wc.cbClsExtra    = 0;
  135.       wc.cbWndExtra    = sizeof( VOID NEAR * );
  136.       wc.hInstance     = g.hInstance;
  137.       wc.hIcon         = NULL;
  138.       wc.hCursor       = NULL; // Set to NULL so we can set cursor in the wndproc
  139.       wc.hbrBackground = (HBRUSH) NULL;
  140.       wc.lpszMenuName  = NULL;
  141.       wc.lpszClassName = PLAYER_MOVIE_CLASS;
  142.  
  143.       return RegisterClass( &wc );
  144.      }
  145.  
  146.  
  147. // Function: PlayerInitInst - Perform Instance Initialization
  148. // --------------------------------------------------------------------
  149. // Parameters: HINSTANCE hPrevInstance;  Previous instance
  150. //             LPSTR     lpCmdLine;      -->Command line arguments
  151. //             int       nCmdShow;       Parameter for first ShowWindow()
  152. //
  153. // Returns:    HWND      hwndFrame;      Frame window handle
  154. //                                       or NULL if initialization failed
  155. // --------------------------------------------------------------------
  156.    static HWND NEAR PlayerInitInst
  157.                  ( HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow )
  158.  
  159.      {
  160.    // Do QuickTime Initializations.
  161.       if( !DoQuickTimeInit( hPrevInstance, lpCmdLine, &nCmdShow ) )
  162.           return NULL;
  163.  
  164.    // get menu and accelerators from localized resource
  165.       g.hAccel = LoadAccelerators ( g.hResources,
  166.                                   MAKEINTRESOURCE( PLAYER_ACCELERATORS ));
  167.       g.hMenu  = LoadMenu( g.hResources,
  168.                                   MAKEINTRESOURCE( PLAYER_FRAME_MENU ));
  169.       if( !g.hAccel || !g.hMenu )
  170.          {CommonTellUser( g.hResources, PLAYER_STRING_NOACCELORMENU,
  171.                                           PLAYER_STRING_CAPTION, MB_OK );
  172.           return NULL;
  173.          }
  174.  
  175.    // Create a main window for this application instance.
  176.       if ( !(g.hwndFrame = CreateWindow( PLAYER_FRAME_CLASS, "",
  177.                   WS_OVERLAPPEDWINDOW | WS_CLIPCHILDREN,
  178.                   CW_USEDEFAULT, CW_USEDEFAULT,
  179.                   CW_USEDEFAULT, CW_USEDEFAULT,
  180.                   NULL, g.hMenu, g.hInstance, NULL )))
  181.          {CommonTellUser( g.hResources, PLAYER_STRING_NOWINDOW,
  182.                                         PLAYER_STRING_CAPTION, MB_OK );
  183.           return NULL;
  184.          }
  185.  
  186.    // Tell qtole.dll the server hwnd
  187.       QTOLE_SetApplicationHwnd( &g.qtoleOleData, g.hwndFrame ); 
  188.  
  189.    // get MDI client window created during WM_CREATE message processing
  190.       g.hwndClient = PlayerQueryClientWindow();   // this is in FrameWnd.c
  191.  
  192.    // Display the frame window
  193.    // If ole started the app, the window will be initially hidden
  194.       ShowWindow  ( g.hwndFrame, nCmdShow );
  195.       if( nCmdShow != SW_HIDE )
  196.           UpdateWindow( g.hwndFrame );
  197.  
  198.    // Check command line for movie file.
  199.    // Note: this must come after ShowWindow, UpdateWindow
  200.       if( lpCmdLine = PlayerParseCmdLine( lpCmdLine ))
  201.           SendMessage( g.hwndFrame, WM_PLAYER_CMDLINE,
  202.                                                    0, (LPARAM) lpCmdLine );
  203.  
  204.       return g.hwndFrame;
  205.      }
  206.  
  207. // Function: PlayerParseCmdLine - Parse the command line. Return -> to
  208. //                                first argument and ignore any extras.
  209. //                                The default extension is appended if
  210. //                                name has no extension
  211. // --------------------------------------------------------------------
  212. // Parameters: LPSTR      lpCmdLine     command line pointer
  213. //
  214. // Returns:    LPSTR      lpCmdLine     command line pointer to first
  215. //                                      argument, else NULL
  216. // --------------------------------------------------------------------
  217.    static LPSTR NEAR PlayerParseCmdLine( LPSTR lpCmdLine )
  218.  
  219.      {LPSTR  lpTemp;                          // Temp pointer
  220.       char   szExtension[FILE_EXT_LEN + 1];   // Default file extension
  221.       BOOL   bExtension;                      // Extension flag
  222.  
  223.    // Command line is already in Ansi char set even if entered from DOS
  224.    // command line
  225.  
  226.    // remove any leading blanks
  227.       while( *lpCmdLine == ' ' )
  228.           lpCmdLine = AnsiNext( lpCmdLine );
  229.  
  230.       if( *lpCmdLine == '\0' )
  231.           return NULL;
  232.  
  233.    // look for blank or end of string
  234.       bExtension = FALSE;
  235.       lpTemp = lpCmdLine;
  236.       while( *lpTemp && (*lpTemp != ' ' ))
  237.          {if( *lpTemp == '.' )
  238.               bExtension = TRUE;
  239.           lpTemp = AnsiNext( lpTemp );
  240.          }
  241.       *lpTemp = '\0';
  242.  
  243.       if( !bExtension )
  244.          {LoadString( g.hResources,
  245.                 PLAYER_STRING_FILEEXT, szExtension, sizeof( szExtension ));
  246.           lstrcat( lpCmdLine, szExtension );
  247.          }
  248.  
  249.       return lpCmdLine;
  250.      }
  251.  
  252.  
  253. // Function: PlayerTerminateInst - Terminate Instance
  254. // --------------------------------------------------------------------
  255. // Parameters: VOID
  256. //
  257. // Returns:    Always 0L
  258. // --------------------------------------------------------------------
  259.    static LONG NEAR PlayerTerminateInst( VOID )
  260.  
  261.      {
  262.    // Clean up OLE
  263.       if( g.qtoleOleData.lpqtoleServer )
  264.           QTOLE_OLECleanUp( &g.qtoleOleData );
  265.  
  266.    // Cut the connections to QuickTime.
  267.       KillQuickTime();
  268.  
  269.    // Free the resource-only DLL
  270.       if ( g.hResources && ( g.hInstance != g.hResources ))
  271.           FreeLibrary( g.hResources );
  272.  
  273.       return 0L;
  274.      }
  275.  
  276.  
  277. // The next two functions are used to initialize and kill QuickTime
  278.  
  279. // Function: DoQuickTimeInit - Establishes connections to QuickTime
  280. // --------------------------------------------------------------------
  281. // Parameters: HINSTANCE  hPrevInstance  Previous instance
  282. //             LPSTR      lpCmdLine      -> command line
  283. //             LPINT      lpnCmdShow     Parameter for first ShowWindow()
  284. //
  285. // Returns:    BOOL       TRUE if OK, else FALSE
  286. // --------------------------------------------------------------------
  287.    static BOOL NEAR DoQuickTimeInit
  288.                ( HINSTANCE hPrevInstance, LPSTR lpCmdLine, LPINT lpnCmdShow )
  289.  
  290.      {OSErr       oserr;                  // Temp return value
  291.       WORD        wIDString;              // ID of error message string
  292.       int         cQueue = 64;            // Best queue size
  293.       QTOLE_INIT  qtoleInit;
  294.       QTOLE_ERR   qtole_err;
  295.       char        szCaption[30];
  296.  
  297.  
  298.       LoadString( g.hResources, PLAYER_STRING_CAPTION,
  299.                                        szCaption, sizeof( szCaption ));
  300.  
  301.          // Do OLE initialization before QTInitialize so we
  302.          // don't have to kill qt for install only
  303.       if( !( qtoleInit.fpServerCallBack = (QTOLEPROC) MakeProcInstance( 
  304.                            (FARPROC) QTOLEServerCallBack, g.hInstance )))
  305.          {CommonTellUser( g.hResources, PLAYER_STRING_NOMEMORY,
  306.                                            PLAYER_STRING_CAPTION, MB_OK );
  307.           return FALSE;
  308.          }
  309.       else
  310.          {qtoleInit.lStructSize           = sizeof( qtoleInit );
  311.           qtoleInit.lVersion              = VERSION_1;
  312.           qtoleInit.hInstance             = g.hInstance;
  313.           qtoleInit.hResources            = g.hResources;
  314.           qtoleInit.lpCmdLine             = lpCmdLine;
  315.           qtoleInit.lpClassName           = PLAYER_FRAME_CLASS;
  316.           qtoleInit.lpServerCaption       = szCaption;
  317.           qtoleInit.lpnCmdShow            = lpnCmdShow;
  318.           qtoleInit.wIDFirstString        = OLE_STRING_FIRST;
  319.           qtoleInit.wIDFirstDlg           = OLE_DLG_FIRST;
  320.           qtoleInit.bMultipleObjectServer = TRUE;
  321.                      
  322.           g.qtoleOleData.lStructSize      = sizeof( g.qtoleOleData );
  323.           g.qtoleOleData.lVersion         = VERSION_1;
  324.           g.qtoleOleData.wObjectType      = MOVIE_OBJECT;
  325.  
  326.           if( QTOLE_OK != 
  327.                    ( qtole_err = QTOLE_Initialize( &g.qtoleOleData, &qtoleInit )))
  328.              {if( QTOLE_INSTALL_ONLY != qtole_err )
  329.                  CommonTellUser( g.hResources, 
  330.                             PLAYER_STRING_OLEINITFAILED,
  331.                                            PLAYER_STRING_CAPTION, MB_OK );
  332.               return FALSE;
  333.              }
  334.          }
  335.  
  336.  
  337.         // Enlarge message queue to prevent occasional lose of posted messages
  338.       while( SetMessageQueue( cQueue-- ) == 0 );
  339.  
  340.       if( ( oserr = QTInitialize( NULL )) != QTI_OK )
  341.          {switch( oserr )
  342.              {case QTI_FAIL_NOEXIST:
  343.                  wIDString = PLAYER_STRING_QTWNOEXIST;
  344.                  break;
  345.  
  346.               case QTI_FAIL_CORRUPTDLL:
  347.                  wIDString = PLAYER_STRING_QTWBADDLL;
  348.                  break;
  349.  
  350.               case QTI_FAIL_286:
  351.                  wIDString = PLAYER_STRING_QTW286;
  352.                  break;
  353.  
  354.               case QTI_FAIL_WIN30:
  355.                  wIDString = PLAYER_STRING_QTWWIN30;
  356.                  break;
  357.  
  358.               default:
  359.                  wIDString = PLAYER_STRING_QTWFAILED;
  360.                  break;
  361.              }
  362.  
  363.           CommonTellUser( g.hResources, wIDString,
  364.                                          PLAYER_STRING_CAPTION, MB_OK );
  365.           return FALSE;
  366.          }
  367.  
  368.       if( EnterMovies() )
  369.          {CommonTellUser( g.hResources, PLAYER_STRING_ENTMOVFAILED,
  370.                                            PLAYER_STRING_CAPTION, MB_OK );
  371.           return FALSE;
  372.          }
  373.  
  374.       return TRUE;
  375.      }
  376.  
  377. // Function: KillQuickTime - Cuts the connections to QuickTime
  378. // --------------------------------------------------------------------
  379. // Parameters: VOID
  380. //
  381. // Returns:    VOID
  382. // --------------------------------------------------------------------
  383.    static VOID NEAR KillQuickTime( VOID )
  384.  
  385.      {ExitMovies();
  386.       QTTerminate();
  387.  
  388.       return;
  389.      }
  390.  
  391.  
  392. //  This function is called by other modules whenever a modeless dialog
  393. //  is activated or deactivated
  394.  
  395. // Function: PlayerSetActiveModeless - Set handle of active modeless dlg
  396. // --------------------------------------------------------------------
  397. // Parameters: WPARAM   wParam          WA_ flag defined in Windows.h
  398. //             HWND     hModelessDlg    Handle of dlg being activated or
  399. //                                      deactivated
  400. //
  401. // Returns:    VOID
  402. // --------------------------------------------------------------------
  403.    VOID FAR PlayerSetActiveModeless( WPARAM wParam, HWND hModelessDlg )
  404.  
  405.      {if( wParam != WA_INACTIVE )
  406.           g.hActiveModelessDlg = hModelessDlg;
  407.       else if( g.hActiveModelessDlg == hModelessDlg )
  408.           g.hActiveModelessDlg = NULL;
  409.  
  410.       return;
  411.      }
  412.  
  413. // Function: PlayerNoMoreWindow - Sets global handles to NULL. Called during
  414. //                                frame window WM_DESTROY message processing
  415. // --------------------------------------------------------------------
  416. // Parameters: VOID
  417. //
  418. // Returns:    VOID
  419. // --------------------------------------------------------------------
  420.    VOID FAR PlayerNoMoreWindow( VOID )
  421.  
  422.      {g.hwndFrame  = NULL;
  423.       g.hwndClient = NULL;
  424.  
  425.       return;
  426.      }
  427.  
  428. ///  The remaining functions are the query functions called by other modules
  429.  
  430. // Function: PlayerQueryInstance - Query Instance Handle
  431. // --------------------------------------------------------------------
  432. // Parameters: None.
  433. //
  434. // Returns:    HINSTANCE hInstance;    Application instance handle
  435. // --------------------------------------------------------------------
  436.    HINSTANCE FAR PlayerQueryInstance( VOID )
  437.  
  438.      {return g.hInstance;
  439.      }
  440.  
  441. // Function: PlayerQueryResources - Query Resource-Only DLL Handle
  442. // --------------------------------------------------------------------
  443. // Parameters: None.
  444. //
  445. // Returns:    HINSTANCE hResources    Resource-only DLL handle
  446. // --------------------------------------------------------------------
  447.    HINSTANCE FAR PlayerQueryResources( VOID )
  448.  
  449.      {return g.hResources;
  450.      }
  451.  
  452. // Function: PlayerQueryFrameWindow - Query Frame Window Handle
  453. // --------------------------------------------------------------------
  454. // Parameters: None.
  455. //
  456. // Returns:    HWND hwndFrame;          Frame window handle
  457. // --------------------------------------------------------------------
  458.    HWND FAR PlayerQueryFrameWindow( VOID )
  459.  
  460.      {return g.hwndFrame;
  461.      }
  462.  
  463. // Function: PlayerQueryOleData - Query -> to Ole Data struct
  464. // --------------------------------------------------------------------
  465. // Parameters: None.
  466. //
  467. // Returns:    LPQTOLE_OLEDATA        -> ole data struct
  468. // --------------------------------------------------------------------
  469.    LPQTOLE_OLEDATA FAR PlayerQueryOleData( VOID )
  470.  
  471.      {return &g.qtoleOleData;
  472.      }
  473.  
  474.  
  475.